import pytest
import yaml
import time
from pythoncode.calculator import Calculator


def pytest_collection_modifyitems(items):
    """
    测试用例收集完成时，将收集到的item的name和nodeid的中文显示
    :return:
    """
    for item in items:
        item.name = item.name.encode("utf-8").decode("unicode_escape")
        item._nodeid = item.nodeid.encode("utf-8").decode("unicode_escape")


@pytest.fixture(scope="session", autouse=True)
def init_calcu():
    yield Calculator()
    print("测试结束")


@pytest.fixture(scope="function", autouse=True)
def start_end_info():
    print("开始计算")
    yield
    print("结束计算")


@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d_%H-%M-%S")
    log_name = 'log/' + now + '.log'

    request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(log_name)


def get_yaml_data(operator, priority, yaml_path=None):
    if not yaml_path:
        yaml_path = r"./../data/data.yml"

    with open(yaml_path, 'r', encoding='utf-8') as f:
        yaml_data = yaml.safe_load(f.read())
    datas = yaml_data.get(operator).get(priority).get("datas")
    ids = yaml_data.get(operator).get(priority).get("ids")
    return datas, ids


@pytest.fixture(params=get_yaml_data("add", "P0")[0], ids=get_yaml_data("add", "P0")[1])
def get_add_p0_data(request):
    return request.param


@pytest.fixture(params=get_yaml_data("add", "P1_1")[0], ids=get_yaml_data("add", "P1_1")[1])
def get_add_p1_1_data(request):
    return request.param


@pytest.fixture(params=get_yaml_data("add", "P1_2")[0], ids=get_yaml_data("add", "P1_2")[1])
def get_add_p1_2_data(request):
    return request.param


@pytest.fixture(params=get_yaml_data("add", "P2")[0], ids=get_yaml_data("add", "P2")[1])
def get_add_p2_data(request):
    return request.param


@pytest.fixture(params=get_yaml_data("div", "P0")[0], ids=get_yaml_data("div", "P0")[1])
def get_div_p0_data(request):
    return request.param

@pytest.fixture(params=get_yaml_data("div", "P1_1")[0], ids=get_yaml_data("div", "P1_1")[1])
def get_div_p1_1_data(request):
    return request.param

@pytest.fixture(params=get_yaml_data("div", "P1_2")[0], ids=get_yaml_data("div", "P1_2")[1])
def get_div_p1_2_data(request):
    return request.param

@pytest.fixture(params=get_yaml_data("div", "P1_3")[0], ids=get_yaml_data("div", "P1_3")[1])
def get_div_p1_3_data(request):
    return request.param

@pytest.fixture(params=get_yaml_data("div", "P2")[0], ids=get_yaml_data("div", "P2")[1])
def get_div_p2_data(request):
    return request.param